//JSON objects
entry1 = {name:"zhangsan", age:30}
entry2 = {name:"lisi", age:40}
//with array
entry3 = {name:"zhouwu", age:50, array: [1, 2, 3, 4]}
//nested
entry4 = {name:"wangliu", age:60, nested:{x:1, y:"abc"}}

//insert
db.persons.insert(entry1);
db.persons.insert(entry2);
db.persons.insert(entry3);
db.persons.insert(entry4);

//query
db.persons.find();
db.persons.find({age:{$gt: 40}})

//traverse with cursor
/* Note: The db.collection.find() method returns a cursor to the results; 
 * however, in the mongo shell, if the returned cursor is not assigned 
 * to a variable using the var keyword, then the cursor is automatically 
 * iterated up to 20 times to print up to the first 20 documents that 
 * match the query. The mongo shell will prompt Type it to iterate 
 * another 20 times.
 */
var cursor = db.persons.find();
while(cursor.hasNext()) {
	obj = cursor.next();
	print(obj.name);
}

//traverse, functionally
var cursor2 = db.persons.find();
cursor2.forEach(function(x){
	print(x.name)
});


//query with limit, skip and sort
db.persons.find().limits(2);
db.persons.find().limits(10);
db.persons.find().skip(1);
db.persons.find().sort({age:1, name:-1});

//remove
db.persons.remove({});
db.persons.remove({name:'zhangsan'})

//drop collection
db.persons.drop();
db.runCommand("drop": persons)
